package com.example.easeimpro.controller.activity;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.example.easeimpro.R;
import com.example.easeimpro.controller.adapter.PickContactAdapter;
import com.example.easeimpro.model.bean.Model;
import com.example.easeimpro.model.bean.PickContactInfo;
import com.example.easeimpro.model.bean.UserInfo;

import java.util.ArrayList;
import java.util.List;

public class PickContactActivity extends BaseActivity {
    private TextView mRlPickSave;
    private RecyclerView mRlPickList;
    private PickContactAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pick_contact);
        initView();
        initData();
        initListener();
    }

    private static final String TAG = "PickContactActivity";
    private void initListener() {
        mRlPickSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<PickContactInfo> pickData = adapter.getPickData();
                List<String> list = new ArrayList<>();
                for(PickContactInfo pick : pickData){
                  if(pick.isChecked()){//转换为String
                      list.add(pick.getUser().getName());
                  }
                }
                Intent intent = new Intent(mContext,CreateGroupActivity.class);//创建intent
                intent.putExtra("members",list.toArray(new String[0]));//传递参数
                setResult(RESULT_OK,intent);//发送信息
                finish();//销毁
            }
        });
    }

    private void initData() {
        List<UserInfo> contacts = Model.getInstance().getDbManager().getContactTableDao().getContacts();
        List<PickContactInfo> mPicks = new ArrayList<>();
        if(contacts != null && contacts.size()>0){
            //转换
            for(UserInfo userInfo : contacts){
                PickContactInfo info = new PickContactInfo(userInfo,false);
            mPicks.add(info);
            }
        }
        adapter = new PickContactAdapter(mContext,mPicks);
        mRlPickList.setAdapter(adapter);
        mRlPickList.setLayoutManager(new LinearLayoutManager(mContext));
    }

    private void initView() {
        mRlPickSave = (TextView) findViewById(R.id.rl_pick_save);
        mRlPickList = (RecyclerView) findViewById(R.id.rl_pick_list);
    }


}